<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Org\Util;

/**
 * Stack实现类
 * @category   ORG
 * @package  ORG
 * @subpackage  Util
 * @author    liu21st <liu21st@gmail.com>
 */
class Category {
    // 一维无限分级
    static function UnlimteForLevel($cate,$pid=0,$html='--',$level=0){
      $arr=array(); 
        foreach ($cate as $v){
          if($v['pid']==$pid){
            $v['level']=$level+1;
            $v['html']=str_repeat($html,$level);
            $arr[]=$v;
            $arr=array_merge($arr,self::UnlimteForLevel($cate,$v['id'],$html,$level+1));
          }
        }
        return $arr;
    }
    
    //多维无限分级
    static function UnlimiteForLayer($cate,$pid=0){
        $arr=array();
        foreach ($cate as $v){
            if($v['pid']==$pid){
                $v['child']=self::UnlimiteForLayer($cate,$v['id']);
                $arr[]=$v;  
            }   
        }
        return $arr;
    }
    
    //传递一个子级ID返回所有父级分类
    static function getParents($cate,$id){
        $arr=array();
        foreach ($cate as $v) {
           if($v['id']==$id){
               $arr[]=$v;
               $arr=array_merge(self::getParents($cate, $v['pid']),$arr);
           }
        }
        return $arr;
    }
    
    //传递一个父级ID返回所有子级分类
    static function getChilds($cate,$pid){
        $arr=array();
        foreach($cate as $v){
            if($v['pid']==$pid){
                $arr[]=$v;
                $arr=array_merge($arr,self::getChilds($cate, $v['id']));
            }
            
        }
        return $arr;
    }

    //传递一个父级ID返回所有子级分类ID
    static function getChildsId($cate,$pid){
        $arr=array();
        foreach($cate as $v){
            if($v['pid']==$pid){
                $arr[]=$v['id'];
                $arr=array_merge($arr,self::getChildsId($cate, $v['id']));
            }
            
        }
        return $arr;
    }
    

}
?>